package com.xinyan.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.xinyan.dto.SprayHostDTO;
import com.xinyan.pojo.SprayHost;
import com.xinyan.service.ISprayHostService;
import com.xinyan.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 雾森主机表 前端控制器
 * </p>
 *
 * @author xzlf
 * @since 2024-03-21 23:29:03
 */
@RestController
@RequestMapping("/host")
@Validated
@Slf4j
public class SprayHostController {

    @Autowired
    private ISprayHostService sprayHostService;

    @GetMapping("/getAllHost")
    public R getAllHost(@NotBlank(message = "手机号不能为空") @Length(max = 11, min = 11, message = "手机号必须为11位") String phone) {
        List<SprayHost> hostList = sprayHostService.findAllHost(phone);
        return R.ok(hostList);

    }
    @PostMapping("/addHost")
    public R addHost(@RequestBody List<SprayHost> hostList) {
        if (sprayHostService.addHost(hostList)){
            return R.ok();
        }
        return R.fail("添加失败 ");
}

    @PostMapping("/updateHost")
    public R updateHost(@RequestBody SprayHost sprayHost){
        if (sprayHostService.updateHost(sprayHost)){
            return R.ok();
        }
        return R.fail("添加失败 ");
    }

    @GetMapping("/findByPage")
    public R<PageDTO<SprayHostDTO>> findByPage(long current,long size, String phone){
        log.info("=====接收到参数：{},{},{}",current,size,phone);
        PageDTO<SprayHostDTO> byPage = sprayHostService.findByPage(current, size, phone);
        return R.ok(byPage);
    }
}
